<template>
  <div class="app-container">
    <h2 class="text-center">{{ paperData.title }}</h2>
    <p class="text-center" style="color: #666">{{ paperData.createTime }}</p>

    <el-row :gutter="24" style="margin-top: 50px">
      <el-col :span="8" class="text-center">
        考生姓名：{{ paperData.realName }} ({{ paperData.userName }})
      </el-col>

      <el-col :span="8" class="text-center">
        考试用时：{{ paperData.userTime }}分钟
      </el-col>

      <el-col :span="8" class="text-center">
        考试得分：{{ paperData.userScore }}
      </el-col>
    </el-row>

    <el-card style="margin-top: 20px">
      <div
        v-for="(item, index) in paperData.quList"
        class="qu-content"
        :key="index"
      >
        <p>题目{{ item.sort + 1 }}</p>
        <div style="margin-bottom: 20px">
          <Markdown
            :value="item.content"
            :height="150"
            :isPreview="true"
          ></Markdown>
        </div>

        <!-- 选择题部分 -->
        <div v-if="item.quType === 1 || item.quType === 3">
          <el-radio-group v-model="radioValues[item.id]">
            <el-radio
              v-for="(an, index) in item.answerList"
              :label="an.id"
              :key="index"
              >{{ an.abc }}.{{ an.content }}</el-radio
            >
          </el-radio-group>

          <el-row :gutter="24">
            <el-col :span="12" style="color: #24da70">
              正确答案：{{ radioRights[item.id] }}
            </el-col>

            <el-col
              v-if="!item.answered"
              :span="12"
              style="text-align: right; color: #ff0000"
            >
              答题结果：未答
            </el-col>

            <el-col
              v-if="item.answered && !item.isRight"
              :span="12"
              style="text-align: right; color: #ff0000"
            >
              答题结果：{{ myRadio[item.id] }}
            </el-col>

            <el-col
              v-if="item.answered && item.isRight"
              :span="12"
              style="text-align: right; color: #24da70"
            >
              答题结果：{{ myRadio[item.id] }}
            </el-col>
          </el-row>
        </div>

        <!-- 简答题部分 -->
        <div v-if="item.quType === 4">
          <el-row :gutter="24">
            <el-col v-if="item.answer" :span="24">
              <div class="answer-box">
                <h4>考生答案：</h4>
                <p>{{ item.answer }}</p>
              </div>
            </el-col>

            <el-col v-if="!item.answer" :span="24" style="color: #ff0000">
              <div class="answer-box">
                <h4>考生答案：</h4>
                <p>未作答</p>
              </div>
            </el-col>

            <el-col :span="24" class="scoring-section">
              <div class="score-box">
                <h4>评分区域：</h4>
                <el-form :model="scoreForm[item.id]" label-width="100px">
                  <el-form-item label="答案评判">
                    <el-radio-group v-model="item.isRight" @click="handleRadioChange">
                      <el-radio :label="true">正确</el-radio>
                      <el-radio :label="false">错误</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-form>
              </div>
            </el-col>
          </el-row>
        </div>

        <!-- 多选题部分 -->
        <div v-if="item.quType === 2">
          <el-checkbox-group v-model="multiValues[item.id]">
            <el-checkbox
              v-for="an in item.answerList"
              :label="an.id"
              :key="an.id"
              >{{ an.abc }}.{{ an.content }}</el-checkbox
            >
          </el-checkbox-group>

          <el-row :gutter="24">
            <el-col :span="12" style="color: #24da70">
              正确答案：{{ multiRights[item.id].join(",") }}
            </el-col>

            <el-col
              v-if="!item.answered"
              :span="12"
              style="text-align: right; color: #ff0000"
            >
              答题结果：未答
            </el-col>

            <el-col
              v-if="item.answered && !item.isRight"
              :span="12"
              style="text-align: right; color: #ff0000"
            >
              答题结果：{{ myMulti[item.id].join(",") }}
            </el-col>

            <el-col
              v-if="item.answered && item.isRight"
              :span="12"
              style="text-align: right; color: #24da70"
            >
              答题结果：{{ myMulti[item.id].join(",") }}
            </el-col>
          </el-row>
        </div>
      </div>

      <div class="submit-section">
        <el-button type="primary" @click="submitScores">提交评分</el-button>
      </div>
    </el-card>
  </div>
</template>
  
  <script>
import { paperResult } from "@/api/paper/exam";
import { reviewPaper } from "@/api/paper/paper";
import router from "@/router";
import Markdown from "vue-meditor";

export default {
  name: "CorrectPaler",
  components: { Markdown },
  data() {
    return {
      // 试卷ID
      paperId: "",
      paperData: {
        quList: [],
      },
      radioValues: {},
      multiValues: {},
      radioRights: {},
      multiRights: {},
      myRadio: {},
      myMulti: {},
      // 评分表单
      scoreForm: {},
    };
  },
  created() {
    const id = this.$route.params.id;
    if (typeof id !== "undefined") {
      this.paperId = id;
      this.fetchData(id);
    }
  },
  methods: {
    fetchData(id) {
      const params = { id: id };
      paperResult(params).then((response) => {
        // 试卷内容
        this.paperData = response.data;

        // 初始化评分表单
        this.paperData.quList.forEach((item) => {
          this.scoreForm[item.id] = {
            isCorrect: false,
            score: 0,
            comment: "",
          };
        });

        // 填充该题目的答案
        this.paperData.quList.forEach((item) => {
          let radioValue = "";
          let radioRight = "";
          let myRadio = "";
          const multiValue = [];
          const multiRight = [];
          const myMulti = [];

          item.answerList.forEach((an) => {
            // 用户选定的
            if (an.checked) {
              if (item.quType === 1 || item.quType === 3) {
                radioValue = an.id;
                myRadio = an.abc;
              } else {
                multiValue.push(an.id);
                myMulti.push(an.abc);
              }
            }

            // 正确答案
            if (an.isRight) {
              if (item.quType === 1 || item.quType === 3) {
                radioRight = an.abc;
              } else {
                multiRight.push(an.abc);
              }
            }
          });

          this.multiValues[item.id] = multiValue;
          this.radioValues[item.id] = radioValue;
          this.radioRights[item.id] = radioRight;
          this.multiRights[item.id] = multiRight;
          this.myRadio[item.id] = myRadio;
          this.myMulti[item.id] = myMulti;
        });
      });
    },

    handleScoreChange(questionId) {
      // 确保分数不超过题目满分
      const question = this.paperData.quList.find((q) => q.id === questionId);
      if (this.scoreForm[questionId].score > question.score) {
        this.scoreForm[questionId].score = question.score;
      }
    },

    handleRadioChange(e){
    console.log('e--'+e)
    if (e == true) {
      this.paperData.userScore += 5
    }else{
      this.paperData.userScore -= 5
    }
  },

    submitScores() {
      // 构建提交数据
      // const submitData = {
      //   paperId: this.paperId,
      //   scores: Object.keys(this.scoreForm).map(questionId => ({
      //     questionId,
      //     ...this.scoreForm[questionId]
      //   }))
      // };

      console.log(this.paperData.quList);
      // TODO: 调用API提交评分
      // console.log('提交的评分数据：', submitData);
      const params = { paperId: this.paperId, answers: this.paperData.quList ,totalScore:this.paperData.userScore};

      reviewPaper(params).then((res) => {
        this.$message({
          type: "success",
          message: "评分提交成功！",
        });
        setTimeout(()=>{
            router.push({name:"ReviewPaper"})
        },1000)
      });
    },
  },
};
</script>
  
  <style scoped>
.qu-content {
  border-bottom: #eee 1px solid;
  padding-bottom: 20px;
  margin-bottom: 20px;
}

.qu-content div {
  line-height: 30px;
}

.el-checkbox-group label,
.el-radio-group label {
  width: 100%;
}

.card-title {
  background: #eee;
  line-height: 35px;
  text-align: center;
  font-size: 14px;
}

.card-line {
  padding-left: 10px;
}

.card-line span {
  cursor: pointer;
  margin: 2px;
}

.answer-box {
  background: #f8f9fa;
  padding: 15px;
  border-radius: 4px;
  margin-bottom: 15px;
}

.answer-box h4 {
  margin: 0 0 10px 0;
  color: #333;
}

.score-box {
  background: #fff;
  padding: 15px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

.total-score {
  margin-left: 10px;
  color: #666;
}

.submit-section {
  margin-top: 30px;
  text-align: center;
}

.scoring-section {
  margin-top: 20px;
}
</style>